/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scm.rawMaterialPurchasing.managedbean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.FlowEvent;
import org.primefaces.model.DualListModel;
import scm.rawMaterialPurchasing.entity.RawMaterialEntity;
import scm.rawMaterialPurchasing.entity.SupplierEntity;
import scm.rawMaterialPurchasing.entity.SupplierLineItemEntity;
import scm.rawMaterialPurchasing.session.SupplierSessionBean;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class EditSupplierManagedBean {
    
    private final static Logger logger = Logger.getLogger(CreateNewSupplierManagedBean.class.getName());
    private SupplierEntity supplier;
    private DualListModel rawMaterialDual;
    private List<RawMaterialEntity> target;
    private List<Double> pricing;
    private List<RawMaterialEntity> source;
    private List<SupplierLineItemEntity> supplierLineItemsList;
    private boolean changed=false;
    
    @EJB
    SupplierSessionBean supplierSessionBean;

    /** Creates a new instance of EditSupplierManagedBean */
    public EditSupplierManagedBean() {
        supplierSessionBean = new SupplierSessionBean();
        pricing = new ArrayList<Double>();
    }
    
    public void initView(PhaseEvent event){
        if (getSupplier() == null) {
            setSupplier((SupplierEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("supplier"));
        }
//        supplierLineItemsList = supplier.getSupplierLineItems();
        
    }
    
    public void dualListFunction() {
        System.out.println("&&&&&&&&&&&&"+supplier.getSupplierLineItems().size());
        supplierLineItemsList = supplier.getSupplierLineItems();
        List<RawMaterialEntity> sourceList = supplierSessionBean.getAllRawMaterial();
        for(int p=0;p<supplierLineItemsList.size();p++){
            for(int q=0;q<sourceList.size();q++){
                if(supplierLineItemsList.get(p).getRawMaterial().getRawMaterialName().equals(sourceList.get(q).getRawMaterialName())){
                    sourceList.remove(q);
                    q=q-1;
                }
            }
        }
        setSource(sourceList);
        
        System.out.println("&&&&&&&&&&&&"+supplier.getEmailAddress());
        supplierLineItemsList = supplier.getSupplierLineItems();
        target = new ArrayList<RawMaterialEntity>();
        System.out.println("&&&&&&&&&&&&"+supplierLineItemsList.size());
        for (int i=0;i<supplierLineItemsList.size();i++){
            target.add(supplierLineItemsList.get(i).getRawMaterial());
        }
        rawMaterialDual = new DualListModel(getSource(), target);
    }

    public void dualListComplete(ActionEvent event) {
        supplierLineItemsList = new ArrayList<SupplierLineItemEntity>();
        for (int i = 0; i < rawMaterialDual.getTarget().size(); i++) {
            SupplierLineItemEntity temp = new SupplierLineItemEntity();
            temp.setRawMaterial((RawMaterialEntity) rawMaterialDual.getTarget().get(i));

            for(int p=0;p<supplier.getSupplierLineItems().size();p++){
                if (temp.getRawMaterial().getRawMaterialName().equals(supplier.getSupplierLineItems().get(p).getRawMaterial().getRawMaterialName())){
                    temp.setPrice(supplier.getSupplierLineItems().get(p).getPrice());
                    break;
                }
                else{
                    temp.setPrice(0.0);
                }
            }
            supplierLineItemsList.add(temp);
        }
        changed = true;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Completed. Please proceed", ""));
    }
    
    public String onFlowProcess(FlowEvent event) {
        logger.info("Current wizard step:" + event.getOldStep());
        logger.info("Next step:" + event.getNewStep());

        if(event.getOldStep().equals("supplierDetail") && event.getNewStep().equals("rawMaterialDetail"))
        {
            dualListFunction();
        }

        return event.getNewStep();
    }
    
    public void save(ActionEvent event) throws IOException {
        supplierSessionBean.editSupplier(supplier,supplierLineItemsList,changed);   
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Done Successfully", ""));
    }
    
    public void goBack() throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().redirect("./manageSuppliers.xhtml");
    }

    /**
     * @return the supplier
     */
    public SupplierEntity getSupplier() {
        return supplier;
    }

    /**
     * @param supplier the supplier to set
     */
    public void setSupplier(SupplierEntity supplier) {
        this.supplier = supplier;
    }

    /**
     * @return the rawMaterialDual
     */
    public DualListModel getRawMaterialDual() {
        return rawMaterialDual;
    }

    /**
     * @param rawMaterialDual the rawMaterialDual to set
     */
    public void setRawMaterialDual(DualListModel rawMaterialDual) {
        this.rawMaterialDual = rawMaterialDual;
    }

    /**
     * @return the target
     */
    public List<RawMaterialEntity> getTarget() {
        return target;
    }

    /**
     * @param target the target to set
     */
    public void setTarget(List<RawMaterialEntity> target) {
        this.target = target;
    }

    /**
     * @return the pricing
     */
    public List<Double> getPricing() {
        return pricing;
    }

    /**
     * @param pricing the pricing to set
     */
    public void setPricing(List<Double> pricing) {
        this.pricing = pricing;
    }

    /**
     * @return the source
     */
    public List<RawMaterialEntity> getSource() {
        return source;
    }

    /**
     * @param source the source to set
     */
    public void setSource(List<RawMaterialEntity> source) {
        this.source = source;
    }

    /**
     * @return the supplierLineItemsList
     */
    public List<SupplierLineItemEntity> getSupplierLineItemsList() {
        return supplierLineItemsList;
    }

    /**
     * @param supplierLineItemsList the supplierLineItemsList to set
     */
    public void setSupplierLineItemsList(List<SupplierLineItemEntity> supplierLineItemsList) {
        this.supplierLineItemsList = supplierLineItemsList;
    }
}
